package com.weasel.weixin.mp.builder;

import com.weasel.helper.ClassPathFileReader;
import com.weasel.helper.DocumentConverter;
import com.weasel.weixin.mp.config.MpConfig;

import java.io.IOException;

/**
 * Created by Dylan on 2015/9/28.
 */
public final class MpConfigBuilder {



    private MpConfig config = new MpConfig();

    public static MpConfigBuilder begin(){

        return new MpConfigBuilder();
    }

    public static MpConfig fromXML(String xmlPath) throws IOException {
        MpConfigBuilder builder = new MpConfigBuilder();

        String xml = ClassPathFileReader.readToString(xmlPath);
        builder.config = DocumentConverter.xmlToEntity(xml,MpConfig.class);
        return builder.config;
    }

    public MpConfigBuilder withToken(String token){

        config.setToken(token);
        return this;
    }

    public MpConfigBuilder withAppid(String appid){

        config.setAppid(appid);
        return this;
    }

    public MpConfigBuilder withSecret(String secret){

        config.setSecret(secret);
        return this;
    }

    public MpConfigBuilder withEncodingAESKey(String encodingAESKey){
        config.setEncodingAESKey(encodingAESKey);
        return this;
    }

    public MpConfigBuilder withOauth2redirectUri(String oauth2redirectUri){
        config.setOauth2redirectUri(oauth2redirectUri);
        return this;
    }

    public MpConfig end(){
        return config;
    }



    private MpConfigBuilder(){}
}
